import java.io.IOException;
import java.util.concurrent.*;

/**
 * @author hanchao
 * @date 2017/10/3 14:48
 */
public class Main {
    public static void main(String[] args) throws IOException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,5, TimeUnit.MINUTES, new SynchronousQueue<>(), Executors.defaultThreadFactory(),new BlockingPolicy());


        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            threadPoolExecutor.execute(()->{
                try {
                    Thread.sleep(ThreadLocalRandom.current().nextInt(5000)+2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ok");
            });
        }
    }



    public static class BlockingPolicy implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(r);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
